package com.smforj.ssm.modules.lib.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smforj.ssm.frame.core.dao.BaseDao;
import com.smforj.ssm.frame.core.service.impl.BaseServiceImpl;
import com.smforj.ssm.modules.lib.constant.RecordType;
import com.smforj.ssm.modules.lib.dao.SysRecordDao;
import com.smforj.ssm.modules.lib.domain.CheckInResponse;
import com.smforj.ssm.modules.lib.domain.CheckOutResponse;
import com.smforj.ssm.modules.lib.domain.SysReader;
import com.smforj.ssm.modules.lib.domain.SysRecord;
import com.smforj.ssm.modules.lib.service.SysRecordService;

@Service
public class SysRecordServiceImpl extends BaseServiceImpl<SysRecord> implements SysRecordService {

    @Autowired
    private SysRecordDao sysRecordDao;

    @Override
    protected BaseDao<SysRecord> getBaseDao() {
        return sysRecordDao;
    }

    /**
     * .
     * 保存记录信息
     *
     * @param recordType 记录类型
     * @param responses  请求结果
     * @param sysReader  读者信息
     * @return
     */
    @Override
    public void saveRecord(RecordType recordType, List<?> responses, SysReader sysReader) {
        for (Object response : responses
                ) {
            CheckOutResponse checkOutResponse = (CheckOutResponse) response;
            if (checkOutResponse.getStatus() != 0) {
                SysRecord sysRecord = new SysRecord();
                sysRecord.setCreate_date(new Date(System.currentTimeMillis()));
                sysRecord.setBookId(checkOutResponse.getBookId());
                //todo
                sysRecord.setCreate_by("jade");
                sysRecord.setUpdate_by("jade");
                sysRecord.setRfid(checkOutResponse.getRfid());
                sysRecord.setReaderId(sysReader.getId());
                sysRecord.setRecordType(recordType);
                sysRecordDao.insert(sysRecord);
            }
        }
    }

    /**
     * .
     * 保存记录信息
     *
     * @param recordType 记录类型
     * @param responses  请求结果
     * @return
     */
    @Override
    public void saveRecord(RecordType recordType, List<?> responses) {
        SysRecord sysRecord = new SysRecord();
        for (Object response : responses
                ) {
            CheckInResponse checkInResponse = (CheckInResponse) response;
            if (checkInResponse.getStatus() != 0) {
                sysRecord.setBookId(checkInResponse.getBookId());
                sysRecord.setRfid(checkInResponse.getRfid());
                //todo
                sysRecord.setCreate_by("test");
                sysRecord.setUpdate_by("test");
//            sysRecord.setReaderId();
                sysRecord.setRecordType(recordType);
                sysRecord.setCreate_date(new Date(System.currentTimeMillis()));
                sysRecordDao.insert(sysRecord);
            }
        }
    }
}
